//
//  TabBarController.swift
//  MyWeibo
//
//  Created by stone on 15/7/27.
//  Copyright © 2015年 gff. All rights reserved.
//

import UIKit

class TabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()

        tabBar.tintColor = UIColor.orangeColor()
       addChildViewControllers()
    }
    //MARK: - 添加子控制器
   ///添加多个子控制器
    private func addChildViewControllers() {
        addChildViewController(HomeTableViewController(), title: "首页",imageName:"tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息",imageName:"tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现",imageName:"tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我",imageName:"tabbar_profile")
    }

    ///添加子控制器
   private func addChildViewController(viewController: UIViewController,title:String,imageName:String) {
        viewController.title = title
        let nav = UINavigationController(rootViewController: viewController)
        viewController.tabBarItem.image = UIImage(named: imageName)
        addChildViewController(nav)
    }
    //MARK: - 添加中间add button
    ///调用设置frame的方法
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        setMainButton()
    }
    ///设置中间button的frame
    func setMainButton()
    {
        mainAddButton.frame = CGRectMake(0, 0, tabBar.bounds.size.width / 5 , tabBar.bounds.size.height)
        mainAddButton.center = CGPoint(x: tabBar.center.x, y: tabBar.bounds.size.height * 0.5)
    }
    ///添加中间的button-懒加载
    lazy private var mainAddButton: UIButton =
    {
        let button = UIButton(type: UIButtonType.Custom)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
         button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        self.tabBar.addSubview(button)
        return button
    }()
    func buttonClick()
    {
        print("点击了中间的 按钮")
    }
}
